#!/bin/bash

#fonts color
Green="\033[32m"
Red="\033[31m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
Font="\033[0m"

#notification information
OK="${Green}[OK]${Font}"
Error="${Red}[错误]${Font}"

cur_path="$(pwd)"
ip_addr="$(curl http://api.ipify.org)"


env_set() {
    local key=$1
    local val=$2
    local exist=`cat ${cur_path}/.env | grep "^$key="`
    if [ -z "$exist" ]; then
        echo "$key=$val" >> $cur_path/.env
    else
        if [[ `uname` == 'Linux' ]]; then
            sed -i "/^${key}=/c\\${key}=${val}" ${cur_path}/.env
        else
            docker run -it --rm -v ${cur_path}:/www alpine sh -c "sed -i "/^${key}=/c\\${key}=${val}" /www/.env"
        fi
        if [ $? -ne  0 ]; then
            error "设置env参数失败！"
            exit 1
        fi
    fi
}

# # # # 
# # # # 
# # # # 

# 1
cp .env.docker .env
# 2
env_set WG_HOST "${ip_addr}"
# 3
DOCKER_BUILDKIT=1 docker build --tag wfs-wg-easy .
# 4
apt install docker-compose
# 5
docker-compose up -d

# 完成
echo -e "${OK} ${GreenBG} 安装完成 ${Font}"
echo -e "地址:${GreenBG}http://${ip_addr}:51821${Font}"